// ActionScript file
import com.newland.flow.model.NodeUtils;
import com.newland.flow.model.ProcessModuleLocator;
import com.newland.flow.view.BaseNodeView;
import com.newland.flow.view.EndNodeView;
import com.newland.flow.view.LinkNodeView;
import com.newland.flow.view.StartNodeView;
import com.newland.flow.view.TaskNodeView;

import flash.events.MouseEvent;

import mx.containers.Panel;
import mx.core.UIComponent;

[Embed('asserts/icon/tool/start.gif')]
[Bindable]
private var startIcon:Class;
[Embed('asserts/icon/tool/activity.gif')]
[Bindable]
private var taskIcon:Class;
[Embed('asserts/icon/tool/end.gif')]
[Bindable]
private var endIcon:Class;
[Embed('asserts/icon/tool/link.gif')]
[Bindable]
private var linkIcon:Class;
[Bindable]
public var processModule:ProcessModuleLocator = ProcessModuleLocator.getInstance();

private function init():void {
	//初始化节点
	var startNode:StartNodeView = new StartNodeView(startIcon,"开始节点");
	startNode.addEventListener(MouseEvent.CLICK,onClick);
	//任务节点
	var taskNode:TaskNodeView = new TaskNodeView(taskIcon,"任务节点");
	taskNode.addEventListener(MouseEvent.CLICK,onClick);
	//结束节点
	var endNode:EndNodeView = new EndNodeView(endIcon,"结束节点");
	endNode.addEventListener(MouseEvent.CLICK,onClick);
	//链接线
	var linkNode:LinkNodeView = new LinkNodeView(linkIcon," 连接线 ");
	linkNode.addEventListener(MouseEvent.CLICK,onClick);
	//鼠标移入、移除效果
	startNode.addEventListener(MouseEvent.ROLL_OVER,rollOver);
	startNode.addEventListener(MouseEvent.ROLL_OUT,rollOut);
	taskNode.addEventListener(MouseEvent.ROLL_OVER,rollOver);
	taskNode.addEventListener(MouseEvent.ROLL_OUT,rollOut);
	endNode.addEventListener(MouseEvent.ROLL_OVER,rollOver);
	endNode.addEventListener(MouseEvent.ROLL_OUT,rollOut);
	//注册组件
	box.addChild(startNode);
	box.addChild(taskNode);
	box.addChild(endNode);
	box.addChild(linkNode);
}
public function onClick(event:MouseEvent):void {
	if(processModule.linkObjectCollection.length > 0) {
		for(var i:Number=0;i<processModule.linkObjectCollection.length;i++) {
			UIComponent(processModule.linkObjectCollection.getItemAt(i)).clearStyle("backgroundColor");
		}
		processModule.linkObjectCollection.removeAll();//确保彻底清除临时链接对象集
	}
	var node:UIComponent = event.currentTarget as UIComponent;
	/**通过两次点击对象的isClicked判断**/
	if(!BaseNodeView(node).isClicked) {//按下效果
		BaseNodeView(node).isClicked = true;
		node.setStyle("backgroundColor","#9adae8");
		node.removeEventListener(MouseEvent.ROLL_OUT,rollOut);
		node.removeEventListener(MouseEvent.ROLL_OVER,rollOver);
		/**根据触发不同动作的组件，设置不同的响应**/
		if(node is StartNodeView) {
			processModule.nodeType = NodeUtils.STARTNODE;
		}
		if(node is TaskNodeView) {
			processModule.nodeType = NodeUtils.TASKNODE;
		}
		if(node is EndNodeView) {
			processModule.nodeType = NodeUtils.ENDNODE;
		}
		if(node is LinkNodeView) {
			processModule.nodeType = NodeUtils.LINKLINE;
		}
		//释放上次点击按键的设置
		if(processModule.lastClickNode != null) {
			BaseNodeView(processModule.lastClickNode).isClicked = false;
			processModule.lastClickNode.setStyle("backgroundColor","#ffffff");
			processModule.lastClickNode.addEventListener(MouseEvent.ROLL_OVER,rollOver);
			processModule.lastClickNode.addEventListener(MouseEvent.ROLL_OUT,rollOut);
		}
		processModule.lastClickNode = event.currentTarget as UIComponent;
	} else {//再次按下效果
		BaseNodeView(node).isClicked = false;
		node.setStyle("backgroundColor","#ffffff");
		node.addEventListener(MouseEvent.ROLL_OVER,rollOver);
		node.addEventListener(MouseEvent.ROLL_OUT,rollOut);
		processModule.lastClickNode = null;
		processModule.nodeType = "";
	}
}
/**
 * 节点选择鼠标移入效果
 **/
private function rollOver(event:MouseEvent):void {
	var target:UIComponent = event.currentTarget as UIComponent;
	target.setStyle("backgroundColor","#9adae8");
}
/**
 * 节点选择鼠标移出效果
 **/
private function rollOut(event:MouseEvent):void {
	var target:UIComponent = event.currentTarget as UIComponent;
	target.setStyle("backgroundColor","#ffffff");
}